<?php
class Categorie{
	private $id;
	private $parentCatId;
	private $categories;
	private $documents;
	private $defaultDocument;
	private $infos;
	private $is_root;

	function __construct($id) {

		if (is_int($id)){
			if($id >= 0){
				$this->id=$id;
			}else{
				throw new FieldFormatException($_SESSION['translator']->idVal);
			}
		}else{
			throw new FieldFormatException($_SESSION['translator']->idInt);
		}
	}
	public function getId(){
		return $this->id;
	}
	public function getInfos(){
		return $this->infos;
	}
	public function getTooltip($iso){
		$tooltip = "";
		foreach ($this->getInfos() as $Catinf){
			if(strtolower($Catinf->getLangue()->getIso()) == strtolower($iso)){
				$tooltip = $Catinf->getTooltip();
			}
		}
		return $tooltip;
	}
	public function getLabel($iso){
		$label = "";
		foreach ($this->getInfos() as $Catinf){

			if(strtolower($Catinf->getLangue()->getIso()) == strtolower($iso)){
				$label = $Catinf->getLabel();
			}
		}
		return $label;
	}
	public function setInfos($infoTab){
		if(is_array($infoTab)){
			$bool = true;
			foreach ($infoTab as $info){
				if  (!is_a($info, 'CategorieInfo')){
					$bool=false;
				}
			}
			if (!$bool){
				throw new FieldFormatException($_SESSION['translator']->listEx." CategorieInfo");
			}else{
				$this->infos=$infoTab;
			}
		}else{
			throw new FieldFormatException($_SESSION['translator']->arrayErr);
		}
	}
	public function setParentCategorieId($parentCatId){
		if (is_int($parentCatId)){
			$this->parentCatId = $parentCatId;
		}
	}
	public function getParentCategorieId(){
		return $this->parentCatId;
	}
	public function hasChildCategorie($catid){
		$bool = false;
		if($catid == $this->getId()){

			$bool = true;
		}else{

			foreach ($this->getChildCategories() as $cat){
				if(!$bool){
					if($cat->getId() == $catid){
						$bool=true;
					}else{
						$bool = $cat->hasChildCategorie($catid);
					}
				}
			}
		}
		return $bool;
	}
	public function containsDocument($docid){
		$bool = false;
		foreach ($this->getChildDocuments() as $doc){
			if($doc->getId() == $docid){
				$bool = true;
			}
		}
		return $bool;
	}
	public function hasChildDocument($docId){
		$bool = false;
		foreach ($this->getChildDocuments() as $tmpdoc){
			if(!$bool){
				if($tmpdoc->getId() == $docId){
					$bool=true;
				}
			}
		}
		if(!$bool){
			foreach ($this->getChildCategories() as $tmpcat){
				if(!$bool){
					$bool = $tmpcat->hasChildDocument($docId);
				}
			}
		}
		return $bool;
	}
	public function setChildCategories($catTab){
		if(is_array($catTab)){
			$bool = True;
			foreach ($catTab as $cat){
				if(!is_a($cat, 'Categorie')){
					$bool = False;
				}
			}
			if ($bool){
				$this->categories = $catTab;
			}else{
				throw new FieldFormatException($_SESSION['translator']->listEx." Categorie");
			}
		}else{
			throw new FieldFormatException($_SESSION['translator']->arrayErr);
		}
	}
	public function getChildCategories(){
		return $this->categories;
	}
	public function getChildCategorie($catId){
		$categ = null;
		foreach ($this->categories as $cat){
			if($cat->id == $catId){
				$categ = $cat;
			}
		}
		return $categ;
	}

	public function setChildDocuments($docTab){
		if(is_array($docTab)){
			$bool = True;
			foreach ($docTab as $doc){
				if(!is_a($doc, 'Document')){
					$bool = False;
				}
			}
			if ($bool){
				$this->documents = $docTab;
			}else{
				throw new FieldFormatException($_SESSION['translator']->listEx." Document");
			}
		}else{
			throw new FieldFormatException($_SESSION['translator']->arrayErr);
		}
	}
	public function getChildDocuments(){
		return $this->documents;
	}
	public function setDefaultDocument($doc){
		if (is_a($doc, 'Document') && $doc->isDefault() && $doc->getParentCategorieId() == $this->id){

			$this->defaultDocument = $doc;
		}else{

			if (is_null($doc)){

				$this->defaultDocument = $doc;
			}else{

				throw new FieldFormatException($_SESSION['translator']->notDefDoc);
			}
		}
	}
	public function getDefaultDocument(){
		return $this->defaultDocument;
	}
	public function isRoot(){
		return $this->is_root;
	}
	public function setRootFlag($bool){
		if($bool==1 || $bool == 0){
			$this->is_root = $bool;
		}else{
			throw new FieldFormatException($_SESSION['translator']->boolErr);
		}
	}

}
?>